﻿/************************************************************************************************************************
 * The game controller, aka. rocking controller
 * detects phone orientation and based on that sets mood and sleepines of baby
 * 
 ************************************************************************************************************************/


using UnityEngine;
using System.Collections;
using System; // for Math.Round();

public class GameController : MonoBehaviour
{

	protected BabyController baby;

	public enum EnvironmentState // represents whether the baby is upside down, slowly or fast rocking
	{
		STILL, // DO NOTHING
		UPSIDE_DOWN,
		UPRIGHT,
		BOTTLE,
		//TICKLE,
		//CARESS,
		//ROCKING_SLOWLY,
		//ROCKING_FAST, // SHAKING
	}

	protected EnvironmentState environmentState = EnvironmentState.STILL;
	protected EnvironmentState oldEnvironmentState;

	protected float upsideDownTimeCounter = 0f; // timer counting how long baby has been upside down (in seconds)
	protected float upsideDownTimeDecreaser = 0f; // decrease to 0 before changing state away from upside down
	protected float upsideDownSecs = 1.0f; // amount of seconds before state can be changed into "upside down"


	protected MoodController moodController;

	// Use this for initialization
	void Start ()
	{
		// find baby
		GameObject babyGO = GameObject.Find("Baby");

		if (baby != null)
		{
			baby = babyGO.GetComponent<BabyController>();
		}

		// initial setup
		//oldY = Input.acceleration.normalized.y;
		oldTime = Time.deltaTime;
		oldEnvironmentState = environmentState;

		moodController = Camera.main.GetComponent<MoodController>();	
	}

	float oldTime = -1f;
	float currentTime = -1f;


	public void ChangeEnvironmentState( EnvironmentState state)
	{
		// currently only used by mouth for feeding
		environmentState = state;

		Debug.Log("called ChangeEnvironmentState, new state : " + state);

		// hack
		//moodController.ChangeMood(environmentState);
		moodController.ChangeMood(environmentState);
	}

	// Update is called once per frame
	void Update ()
	{

		// UPSIDE_DOWN ?

		if( Input.acceleration.y > 0.85f)
		{
			// count x seconds before changing state to "upside down"
			upsideDownTimeCounter += Time.deltaTime;

			if(upsideDownTimeCounter > upsideDownSecs && environmentState != EnvironmentState.UPSIDE_DOWN)
			{
				// we are upside down
				environmentState = EnvironmentState.UPSIDE_DOWN;
				upsideDownTimeDecreaser = upsideDownSecs;

				moodController.ChangeMood(environmentState);
			}
		}

		else
		{
			// if we are upside down we change state

			// change state slowly
			if(environmentState == EnvironmentState.UPSIDE_DOWN)
			{
				upsideDownTimeDecreaser -= Time.deltaTime;

				if(upsideDownTimeDecreaser < 0f)
				{
					upsideDownTimeCounter = 0f; // reset time counter
					environmentState = EnvironmentState.STILL;

					moodController.ChangeMood(environmentState);
				}
			}


			// check if we are upside down instead
			// UPRIGHT?
			/*
			else if( Input.acceleration.y <  -0.85f)
			{
				Debug.Log(Input.acceleration.y);
				environmentState = EnvironmentState.UPRIGHT;
			}
			else if(environmentState == EnvironmentState.UPRIGHT)
			{
				Debug.Log(Input.acceleration.y + "now still");
				environmentState = EnvironmentState.STILL;
			}
			*/

		}


		// EATING: implemented in ClickableMouth

		// Debug.Log ("Environment State = " + environmentState + "\n");

		// Tell mood controller if we have changed state
		/*
		if( environmentState != oldEnvironmentState)
		{
			// notify mood controller
			moodController.ChangeMood(environmentState);
		}
		*/
	}




	// BAD BAD BAD: Old update which was using also rocking controller (attempt to make it)


	/*
	 * 
	// Detect rocking variables
	float oldY = 0f;
	float newY = 0f;
	float peak0Y = 0f;
	float peak1Y = 0f;
	float peak0time = -1f;
	float peak1time = -1f;
	float deltaY = 0f; // space interval between peaks
	float deltaTime = -1f; // time interval between peaks



	// Update is called once per frame
	void Update ()
	{
		// Debug.Log("Input.acceleration.normalized: " + Input.acceleration.normalized );
		
		currentTime += Time.deltaTime;
		
		//Debug.Log("Global Time = " + Math.Round( currentTime, 2)/ * + "\t  X: " + Math.Round( Input.acceleration.x, 2)* / + "\t  Y: " + Math.Round(Input.acceleration.y, 2)/* + "\t  Z: " +  Input.acceleration.z * /);
		
		// Detect Environment State (are we rocking baby? are we shaking it?
		
		// ROCKING VS SHAKING
		
		// Detect y peaks
		//newY =  (float) Math.Round( Input.acceleration.normalized.y, 2);
		
		newY = (float) Math.Round(  (Input.acceleration.normalized.y * 0.1f + oldY * 0.9f)  , 2) ;
		
		
		//Debug.Log("new Y = " + newY + ", old Y = " + oldY);
		
		// do we have a peak?
		if( (oldY > newY && oldY > 0) || (oldY < newY && oldY < 0) )
		{
			//Debug.LogError("We have a peak!!!");
			
			
		}
		
		
		
		// UPSIDE_DOWN
		
		if( Input.acceleration.y > 0.85f)
		{
			// count x seconds before changing state to "upside down"
			upsideDownTimeCounter += Time.deltaTime;
			
			if(upsideDownTimeCounter > upsideDownSecs)
			{
				// we are upside down
				environmentState = EnvironmentState.UPSIDE_DOWN;
				upsideDownTimeDecreaser = upsideDownSecs;
			}
		}
		
		else
		{
			// TODO: if we are upside down we change state
			
			// change state slowly
			if(environmentState == EnvironmentState.UPSIDE_DOWN)
			{
				upsideDownTimeDecreaser -= Time.deltaTime;
				
				if(upsideDownTimeDecreaser < 0f)
				{
					upsideDownTimeCounter = 0f; // reset time counter
					environmentState = EnvironmentState.STILL;
				}
			}
		}
		
		
		// set variables
		oldY = newY;
		oldTime = currentTime;
		
		// Debug.Log ("Environment State = " + environmentState + "\n");
	}
	*/

}









